package tommy.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity; 
import android.os.Bundle; 
import android.view.*; 
import android.graphics.*; 
import android.content.*; 

public class tommy extends Activity 
{    
     public static final int DIRECTION_RIGHT = 0, DIRECTION_LEFT = 1; 
      
     private Panel main; 
     private Bitmap scratch; 
     private Canvas offscreen; 
      
     public boolean start = true; 
     private volatile boolean running = true; 
     private int direction = DIRECTION_RIGHT; 
      
     private int box = 10; 
      
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState);   
        
        setOffscreenBitmap(); 
        
        main = new Panel(this);         
        setContentView(main,new ViewGroup.LayoutParams(320,480)); 
        
        (new Thread(new AnimationLoop())).start(); 
    } 
    
    private void setOffscreenBitmap() 
    { 
//    	Bitmap bmp = Bitmap.
//      scratch = Bitmap.createBitmap(30,30,Bitmap.Config.ARGB_8888); 
      scratch = BitmapFactory.decodeFile("ship.png"); 
      offscreen = new Canvas(); 
      offscreen.setBitmap(scratch); 
      offscreen.drawColor(Color.RED); 
    } 
    
    private synchronized void updatePhysics() 
    { 
     if(box < 10) 
     { 
          direction = DIRECTION_RIGHT; 
     } 
     else if(box > 250) 
     { 
          direction = DIRECTION_LEFT; 
     } 
      
     if(direction == DIRECTION_RIGHT) 
     { 
          box += 10; 
     } 
     else 
     { 
          box -= 10; 
     } 
    } 
    
    private synchronized void doDraw(Canvas canvas, Paint paint) 
    { 
     if(start) 
          { 
               canvas.drawColor(Color.BLACK); 
               canvas.drawBitmap(scratch,10,10,paint); 
               start = false; 
          } 
          else 
          {               
          canvas.save(); 
          canvas.clipRect(box,8,box+32,40);             
          canvas.drawColor(Color.RED); 
          // canvas.drawBitmap(scratch,box,10,paint); 
          canvas.restore();              
          } 
    } 
    
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 
     if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) 
     { 
          if(running) 
          { 
               running = false; 
          } 
          else 
          { 
               running = true; 
          } 
     } 
     else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) 
     { 
          finish(); 
     } 
      
     return true; 
    } 
    
    class Panel extends View 
    { 
     Paint paint; 
      
     public Panel(Context context) 
     { 
          super(context); 
          paint = new Paint(); 
     } 
      
     @Override 
     protected void onDraw(Canvas canvas) 
     { 
          doDraw(canvas,paint); 
     } 
    } 
    
    class AnimationLoop implements Runnable 
    { 
     public void run() 
     { 
          while(true) 
          { 
               while(running) 
               {                              
                    try 
                    { 
                         Thread.sleep(30); 
                    } 
                    catch(InterruptedException ex) {} 
                     
                    updatePhysics(); 
                    main.postInvalidate(); 
               } 
          } 
     } 
    }     
}